﻿using DBServer.Selenium.Extenium.Exception;
using Selenium;
using DBServer.Selenium.Extenium.Path;

namespace DBServer.Selenium.Extenium.Fixtures
{
    public class FrameFixture
    {
        protected ISelenium Driver;
        protected string ExtPath;

        public FrameFixture(ISelenium driver, int index)
        {
            Driver = driver;

            var framePath = "window.frames[" + index + "]";
            if (!PathExists(framePath))
            {
                throw new ExteniumException("Frame doesn't exists. Path not found: " + framePath);
            }

            var extWithinFramePath = framePath + ".Ext";
            if (!PathExists(extWithinFramePath))
            {
                throw new ExteniumException("Frame doesn't contains an Ext object. Path not found: " + extWithinFramePath);
            }

            ExtPath = extWithinFramePath;
        }

        private bool PathExists(string path)
        {
            var eval = Driver.GetEval(path);
            return (eval != "null" && eval != "undefined");
        }

        public ButtonFixture Button(string id)
        {
            return new ButtonFixture(Driver, new ExtComponentPath(id, ExtPath));
        }

    }
}
